/**
 * Created by rockyren on 14/12/3.
 */

describe('Unit: personal控制器',function(){
  beforeEach(module('personalApp'));
  var $httpBackend,$controller,$rootScope;
  beforeEach(inject(function(_$httpBackend_,_$controller_,_$rootScope_){
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;
    $rootScope = _$rootScope_;
  }));

  describe('baseinfoController',function(){
    var baseinfoController,scope,
      resInfo = {
        username: 'Rocky',
        email: 'rocky@example.com',
        realname: 'ren',
        sex: 'M',
        birthday: '2012-2-28',
        nickname: 'bread'

      };
    beforeEach(function(){
      scope = $rootScope.$new();
      baseinfoController = $controller('baseinfoController',{$scope: scope});

      $httpBackend.expectGET('api/personal/baseinfo').respond(200,resInfo);
      $httpBackend.flush();



    });
    it('加载完控制器后,应该能获得个人资料数据',function(){
      expect(scope.info).toEqual(resInfo);
      expect(scope.birthday).toEqual({
        year: 2012,
        month: 2,
        day: 28
      });
    });

    it('日期管理dateHandle测试',function(){
      var yearList = [], dayList = [];

      /*getYearList测试*/
      var today = new Date();
      for(var i=1930;i<=today.getFullYear();i++) {
        yearList.push(i);
      }
      expect(scope.dateHandle.getYearList()).toEqual(yearList);

      /*getMonthList测试*/
      expect(scope.dateHandle.getMonthList()).toEqual([1,2,3,4,5,6,7,8,9,10,11,12]);

      /*getDayList测试*/
      for(var i=1;i<=29;i++){
        dayList.push(i);
      }
      expect(scope.dateHandle.getDayList(2012,2)).toEqual(dayList);
    });

    it('测试方法: submit',function(){

      $httpBackend.expectPUT('api/personal/baseinfo').respond(200, {});
      $httpBackend.expectGET('api/personal/baseinfo').respond(200,resInfo);

      scope.submit();
      $httpBackend.flush();
    });


  });

  /*
  describe('addressController',function(){
    var addressController,scope,
      baseAddressUrl = 'api/personal/address',
      resAddress = {
        defaultAddress: 1,
        addressList:
          [
            {
              id: 10,
              name: 'Rocky',
              location: 'all over the earth',
              district: 'guangdongguangzhou',
              phone: '12345678910',
              postcode: '590000'

            },
            {
              id: 11,
              name: 'Ben',
              location: 'all over the skty',
              district: 'guangdongguangzhou',
              phone: '12345678923',
              postcode: '590000'
            }
          ]
      };


    //创建一个模拟modal对象
    var fakeModal = {
      result: {
        then: function(confirmCallback,cancelCallback) {
          this.confirmCallback = confirmCallback;
          this.cancelCallback = cancelCallback;
        }
      },
      close: function(item) {
        this.result.confirmCallback(item);
      },
      dismiss: function(type) {
        this.result.cancelCallback(type);
      }
    };
    beforeEach(inject(function($modal){
      //$modal监听
      spyOn($modal,'open').andReturn(fakeModal);
    }));

    beforeEach(inject(function(_$modal_){


      scope = $rootScope.$new();
      addressController = $controller('addressController',{
        $scope: scope,
        $modal: _$modal_
      });

      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      $httpBackend.flush();


    }));
    it('加载控制器后,能获得收货地址列表',function(){
      expect(scope.addressList).toEqual(resAddress.addressList);
    });

    it('测试方法: delete',function(){
      $httpBackend.expectDELETE(baseAddressUrl + '/10').respond(200);
      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      scope.delete(10);

      $httpBackend.flush();
    });


    it('调用createAddress后,触发close后,应该能请求创建一个地址',function(){

      scope.createAddress();

      var addressInfo = {
        name: 'Rocky',
        district: 'diqu',
        location: 'university',
        phone: '12345678910',
        postcode: 590000
      };

      $httpBackend.expectPOST(baseAddressUrl,addressInfo).respond(200);
      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      scope.modalInstance.close(addressInfo);
      $httpBackend.flush();
    });

    it('调用editAddress后,触发close后,应该能请求修改一个地址',function(){
      scope.editAddress(12);
      var addressInfo = {
        name: 'Rocky',
        district: 'diqu',
        location: 'university',
        phone: '12345678910',
        postcode: 590000
      };

      $httpBackend.expectPUT(baseAddressUrl + '/12',addressInfo).respond(200);
      $httpBackend.expectGET(baseAddressUrl).respond(200,resAddress);
      scope.modalInstance.close(addressInfo);
      $httpBackend.flush();
    });
  });


  describe('addressModalController',function(){
    var addressModalController,scope,modalInstance;
    beforeEach(function(){
      scope = $rootScope.$new();

      modalInstance = {
        close: jasmine.createSpy('modalInstance.close'),
        dismiss: jasmine.createSpy('modalInstance.dismiss'),
        result: {
          then: jasmine.createSpy('modalInstance.result.then')
        }
      };
      addressModalController = $controller('addressModalController',{
        $scope: scope,
        $modalInstance: modalInstance,
        addressList: null
      });

    });

    it('测试districtListHandle',function(){
      var resPro = [
        {
          id: 1,
          name: 'guangdong'
        },
        {
          id: 2,
          name: 'shanghai'
        }
      ];

      var resCity = [
        {
          id: 12,
          name: 'guangzhou'
        },
        {
          id: 22,
          name: 'shanghai'
        }
      ];

      var resReg = [
        {
          id: 121,
          name: 'huadu'
        },
        {
          id: 221,
          name: 'modu'
        }
      ];

      $httpBackend.expectGET('api/district').respond(200,resPro);
      //scope.districtListHandle.setProvinceList();
      $httpBackend.flush();
      expect(scope.districtList.provinceList).toEqual(resPro);


      $httpBackend.expectGET('api/district/123').respond(200,resCity);
      scope.districtListHandle.setCityList(123);
      $httpBackend.flush();
      expect(scope.districtList.cityList).toEqual(resCity);

      $httpBackend.expectGET('api/district/123/321').respond(200,resReg);
      scope.districtListHandle.setRegionList(123,321);
      $httpBackend.flush();
      expect(scope.districtList.regionList).toEqual(resReg);

    });

  });*/

});